// pages/shangquan/shangquan.js
var app = getApp();
var ddapi = require('../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop:[],
    hangye_xian:[],
    titles1: [
      1,
      2,
    ],
    gengduo:[
      {
        hangye_ico:"",
        hangye_id:0,
        hangye_name:"更多",
        hangye_pic:"https://hongbao.zntech-china.com/attachment/images/wxcx/menu_more.png",
        hangye_small_pic:''
      }
    ],
    hangye_all:[],
    quan:[],
    keyword:'',
    latitude:'',
    longitude:'',
    hangye_id:0,
    len:1000,
    jingwei:'',
    address:'',
    quan_name:'生活圈',
    quan_id:'',
    quan_len:'',
    quan_center:'',
    show_id:'',
    flag:true,
    openid: '',
    user_id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  onShow: function (e) {
    var that = this
    wx.getLocation({
      type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function (res) {
        console.log(res)
        var user_jingwei = wx.getStorageSync('user_jingwei');
        if (user_jingwei == ''){
          that.setData({
            latitude: res.latitude,
            longitude: res.longitude,
          });
        }else{
          var scene = app.globalData.scene;
          console.log(scene);
          console.log('场景值');
          if (scene == 1089){
            that.setData({
              latitude: res.latitude,
              longitude: res.longitude,
            });
          }else{
            var jingwei = user_jingwei.split(",");
            var receiver_lat = jingwei[0];
            var receiver_lng = jingwei[1];
            that.setData({
              latitude: receiver_lat,
              longitude: receiver_lng,
            });
          }
        }
        wx.setStorageSync('user_jingwei', that.data.latitude + ',' + that.data.longitude);
        wx.showToast({
          title: '加载中',
          icon: 'loading'
        })
        that.getAddress();
        that.getHangye();
        var len = wx.getStorageSync('area_len')
        that.setData({
          openid: wx.getStorageSync('openid'),
          user_id: wx.getStorageSync('user_id'),
          len: len
        })
        that.getAllArea();
        that.getShop();
      }
    });
    
  },
  getAddress: function () {
    var that = this
    var lat = that.data.latitude + '';
    var lng = that.data.longitude + '';
    var service = 'Shop.GetAddress';
    var sign = ddapi.md5(lat + lng + service);

    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.GetAddress',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        lat: lat,
        lng: lng
      },

      success: function (res) {
        wx.hideToast()
        console.log(res)
        var info = res.data.data.info
        that.setData({
          address: info.address
        });
      }
    })
  },
  getShop:function(){
    var that = this
    var center_jingwei = that.data.latitude + ',' + that.data.longitude;
    var hangye_id = that.data.hangye_id;
    var length = that.data.len;
    var openid = wx.getStorageSync('openid');
    var service = 'Shop.NewShop';
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(center_jingwei + hangye_id + length + openid + service + user_id);

    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.NewShop',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        center_jingwei: center_jingwei,
        hangye_id: hangye_id,
        length: length,
        openid: openid,
        user_id: user_id
      },
      success: function (res3) {
        wx.hideToast() 
        console.log(res3)
        var shop = res3.data.data.info
        if (res3.data.data.info.length==0){
          wx.showToast({
            title: '附近没有商家啦',
            image:'../../images/popup-close.png'
          })
        }
        that.setData({
          shop: shop
        });
      },
      fail: function (res) {
        wx.hideToast()
      }
    })
  },
  getHangye:function(){
    var that = this
    var openid = wx.getStorageSync('openid');
    var service = 'Shop.HangYe';
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(openid + service + user_id);

    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.HangYe',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        openid: openid,
        user_id: user_id
      },

      success: function (res4) {
        console.log(res4)
        var hangye = [];
        var hangye_all = [];
        for (var i = 0; i < res4.data.data.info.length;i++){
          hangye_all.push(res4.data.data.info[i]);
          if(i<7){
            hangye.push(res4.data.data.info[i]);
          }
        }
        hangye.push(that.data.gengduo[0])
        that.setData({
          hangye_xian: hangye,
          hangye_all: hangye_all,
        });
      }
    })
  },
  showAllhangye:function(){
    var that = this
    that.setData({
      hangye_xian: that.data.hangye_all, 
    });
  },
  getAllArea:function(){
    var that = this
    var openid = wx.getStorageSync('openid');
    var service = 'User.NewArea';
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(openid + service + user_id);
    var quan = [];
    wx.request({
      url: app.globalData.apiUrl + '?service=User.NewArea',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        open_id: openid,
        user_id: user_id
      },

      success: function (res5) {
        console.log(res5.data.data.info)
        var arr = res5.data.data.info;
        for (var i = 0; i < arr.length; i++) {
          var dic = arr[i];
          if (dic.area_name != null && dic.area_name != '') {
            quan.push(dic)
          }
        }
        that.setData({
          quan: quan
        });
      }
    })
  },
  wxSearchInput: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  wxSearchBlur: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  wxSearchFn: function () {
    wx.showToast({
      title: '搜索中',
      icon: 'loading'
    })
    var that = this
    var center_jingwei  = that.data.latitude + ',' + that.data.longitude;
    var keyword = that.data.keyword;
    var lenth = that.data.len
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var service = 'Shop.NewSearchShop';
    var sign = ddapi.md5(center_jingwei + keyword + lenth + openid + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.NewSearchShop',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        center_jingwei: center_jingwei,
        length:lenth,

        openid: openid,
        user_id: user_id,
        keyword: keyword
      },
      success: function (res) {
        wx.hideToast()
        console.log(res)
        var shop = res.data.data.info
        that.setData({
          shop: shop,
          keyword: ''
        })
      },
      fail : function (res) {
        wx.hideToast()
      }
    })
  },
  toHangyeShop:function(e){
    var that = this
    var hangye_id = e.currentTarget.dataset.id
      wx.showToast({
        title: '加载中',
        icon: 'loading'
      })
      that.setData({
        hangye_id: hangye_id
      })
      that.getShop();
  },
  show:function(){
    var that=this
    var quan_num = that.data.quan.length
    if(quan_num>0){
      that.setData({
        flag: !that.data.flag
      })
    }else{
      wx.showToast({
        title: '暂无商圈',
        image: '../../images/popup-close.png'
      })
    }
    
  },
  hide: function () {
    var that = this
    that.setData({
      flag: true
    })
  },
  show_quan:function(e){
    console.log(e.currentTarget.dataset)
    var that = this
    var latlng = e.currentTarget.dataset.jingwei
    var latlng2 = latlng.split(',')
    that.setData({
      latitude: latlng2[0],
      longitude: latlng2[1],
      len: e.currentTarget.dataset.length,
      address: e.currentTarget.dataset.name,
      quan_name: e.currentTarget.dataset.name,
    })
    that.getShop();
  },
  changeShow:function(e){
    var that = this
    var show_id = e.currentTarget.dataset.id
    that.setData({
      show_id:show_id
    })
  },
  collect:function(e){
    var that = this
    var collect = e.currentTarget.dataset.status
    var shop_id = e.currentTarget.dataset.id
    var service = "Shop.Collect"
    var open_id = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(collect + open_id + service + shop_id + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.Collect',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        collect: collect,
        shop_id: shop_id,
        open_id: open_id,
        user_id: user_id,
      },
      success: function (res) {
        that.getShop();
      }
    })
  },
  toShopDetail:function(e){
    var that = this;
    var user_phone = wx.getStorageSync('userPhone');
    if(user_phone){
      wx.navigateTo({
        url: "/pages/shop-detail/index?id=" + e.currentTarget.dataset.id
      })
    }else{
      var url = '/pages/login/login';
      wx.navigateTo({
        url: url
      })
    }
   //that.getlod();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    that.getShop();
    wx.stopPullDownRefresh()
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getlod: function () {
    var that = this;
    var service = 'User.CheckLand';
    var open_id = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(open_id + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=User.CheckLand',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          if (result.info == 0) {
            app.globalData.user_phone = '';
            wx.clearStorage();
            wx.navigateTo({
              url: "/pages/login/login"
            })
          }
        }
      }
    })
  },
})